<?php
class M_barangmasuk extends CI_Model{
	
	function getlist($end,$start,$filter){
		$sql = "SELECT barangmasuk_id, barangmasuk_date, barangmasuk_ket, m_jeniskegiatan_id, t_pengajuandet_id, m_anggaran_id
				FROM t_barangmasuk";
		$query = $this->db->query($sql);
		$total = $query->num_rows();
		
		if ($filter<>""){
				$sql .=preg_match("/WHERE/i",$sql)? " AND ":" WHERE ";
				$sql .= " (lower(barangmasuk_ket) LIKE '%".addslashes(strtolower($filter))."%' )";
		}
		
		$sql .= " ORDER BY barangmasuk_id ";
		$limit = $sql." LIMIT ".$start.", ".$end;
		$query = $this->db->query($limit);
		$arr = array();
		$i = 0;
		foreach($query->result() as $q){
			
			$sql = "SELECT
					t.barangmasukdet_id, t.t_barangmasuk_id , t.m_barang_id, t.barangmasukdet_qty , t.barangmasukdet_price,
					t.barangmasukdet_ket , b.m_satuan_id , s.satuan_nama
				FROM
					t_barangmasukdet as t
				LEFT JOIN m_barang as b
				ON t.m_barang_id = b.barang_id
				LEFT JOIN m_satuan as s
				ON b.m_satuan_id = s.satuan_id
				WHERE
					t_barangmasuk_id =".$q->barangmasuk_id;
			$query = $this->db->query($sql);
			$result = $query->result();
			$barangmasuk_grid = json_encode($result);
			
			$arr[$i] = $q;
			$arr[$i]->barangmasuk_grid = $barangmasuk_grid;
			$i++;
		}
	
		$final = array(
			'total'   => $total,
			'results' => $arr
		);
		return json_encode($final);
	}
	
	function save($data){
		extract($data);
		$data_submit = array(
			'barangmasuk_date' => $barangmasuk_date,
			'barangmasuk_ket'   => $barangmasuk_ket,
			'm_jeniskegiatan_id' => $m_jeniskegiatan_id,
			't_pengajuandet_id' => $t_pengajuandet_id,
			'm_anggaran_id' => $m_anggaran_id
		);
		$this->db->insert('t_barangmasuk',$data_submit);
		$t_barangmasuk_id = $this->db->insert_id();
		for($i = 0; $i < count($barangmasuk_grid[0]); $i++){
			$data_submit = array(
				't_barangmasuk_id' =>$t_barangmasuk_id ,
				'm_barang_id' => $barangmasuk_grid[0][$i],
				'barangmasukdet_qty' => $barangmasuk_grid[1][$i],
				'barangmasukdet_price' => $barangmasuk_grid[2][$i],
				'barangmasukdet_ket' => $barangmasuk_grid[3][$i]
			);
			$this->db->insert('t_barangmasukdet',$data_submit);
		}
		echo 1;
	}
	
	function update($data){
		extract($data);
		$data_submit = array(
			'barangmasuk_date' => $barangmasuk_date,
			'barangmasuk_ket'   => $barangmasuk_ket,
			'm_jeniskegiatan_id' => $m_jeniskegiatan_id,
			't_pengajuandet_id' => $t_pengajuandet_id,
			'm_anggaran_id' => $m_anggaran_id
		);
		$this->db->where('barangmasuk_id',$barangmasuk_id);
		$this->db->update('t_barangmasuk',$data_submit);
		$t_barangmasuk_id = $barangmasuk_id;
		
		$this->db->where('t_barangmasuk_id',$t_barangmasuk_id);
		$this->db->delete('t_barangmasukdet');
		
		for($i = 0; $i < count($barangmasuk_grid[0]); $i++){
			$data_submit = array(
				't_barangmasuk_id' =>$t_barangmasuk_id ,
				'm_barang_id' => $barangmasuk_grid[0][$i],
				'barangmasukdet_qty' => $barangmasuk_grid[1][$i],
				'barangmasukdet_price' => $barangmasuk_grid[2][$i],
				'barangmasukdet_ket' => $barangmasuk_grid[3][$i]
			);
			$this->db->insert('t_barangmasukdet',$data_submit);
		}
		echo 1;
	}
	
	function delete($data){
		$sql = "DELETE FROM t_barangmasuk WHERE barangmasuk_id = ".$data;
		$this->db->query($sql);
		echo 1;
	}

}